#include "stdafx.h"
#include "ServerCallbackData.h"

ServerCallbackData::ServerCallbackData()
{
	RegisterHandler(0,boost::bind(&ServerCallbackData::phPing_0, this,_1));
}
ServerCallbackData::ServerCallbackData(ISocketServer *pSocketServer)
	:m_pSocketServer(pSocketServer)
{

}

ServerCallbackData::~ServerCallbackData()
{
}
void ServerCallbackData::phPing_0(void *pVoid)
{
}

void ServerCallbackData::OnRun()
{
}
void ServerCallbackData::SendPacket(UInt32 nSocketId,PacketEncoder& encoder)
{
	static ISocketServer* pSocketServer = GetSocketServer();
	void   *p = const_cast<void*>(encoder.GetData());
	pSocketServer->SendData(nSocketId,(char*)p+sizeof(UInt32),encoder.GetDataSize()-sizeof(UInt32));
}
void ServerCallbackData::OnRecvData(UInt32 nSocketId, const void* pData, size_t nDataLen)
{
	PacketDecoder decoder;
	if(decoder.SetData(reinterpret_cast<const char*>(pData), nDataLen + 4) == false)
	{
		LOGGER_ERROR("error :bad request,nDataLen:%d",nDataLen);
		return;
	}

	if(decoder.GetItemCount() < 2)
	{
		LOGGER_ERROR("bad request decoder.GetItemCount() < 2",nDataLen);
		return;
	}

	Int32 type = 0;
	Int32 id = 0;

	if(decoder.GetItem(0, type) == false || decoder.GetItem(1, id) == false)
	{
		LOGGER_ERROR("error :bad request,id,type,nDataLen:%d",nDataLen);
		return;
	}
	//printf("ServerCallbackData OnRecvData type:%d,id:%d\n",type,id);

	if(id < 0 || id >= 65536)
	{
		LOGGER_ERROR("error :method id:%d,is out of range",id);
		return;
	}

	//GameMethod method = singleton<DataServer>::instance().GetMethod(id);
	PacketHandler method = GetHandler(id);
	if(method == NULL)
	{
		#ifdef _WIN32
		LOGGER_ERROR("error :method id:%d,is unimplement",id);
		#endif
		return;
	}
	else
	{
		static PacketHandlerParamData param;
		param.nSocketIdData = nSocketId;
		param.pDecoder = &decoder;
		method((void*)&param);
	}
	//method(nSocketId,&decoder);
}

void ServerCallbackData::OnNewConnection(UInt32 nSocketId,const std::string& strIp)
{
	LOGGER_INFO("OnNewConnection:%d,ip:%s",nSocketId,strIp.c_str());
	singleton<PeerManager>::instance().Add(nSocketId);
}

void ServerCallbackData::OnCloseConnection(UInt32 nSocketId,const std::string& strIp)
{
	LOGGER_INFO("OnCloseConnection:%d,ip:%s",nSocketId,strIp.c_str());
	singleton<PeerManager>::instance().Remove(nSocketId);
	singleton<CharManager>::instance().saveAllCharInfo();
}

void ServerCallbackData::OnStop()
{
}

void ServerCallbackData::OnUpdate()
{
}
void ServerCallbackData::Ping(UInt32 nSocketId)
{
	static PacketEncoder encoder;
	encoder.Clear();
	encoder.AddItem(Int32(0));
	encoder.AddItem(Int32(0));

	static ISocketServer* pSocketServer = GetSocketServer();
	void   *p = const_cast<void*>(encoder.GetData());
	pSocketServer->SendData(nSocketId,(char*)p+sizeof(UInt32),encoder.GetDataSize()-sizeof(UInt32));
}
void SendPacket(UInt32 nSocketId,PacketEncoder& encoder)
{
	singleton<ServerCallbackData>::instance().SendPacket(nSocketId,encoder);
}
/*ISocketServerCallback* CreateServerCallbackData(ISocketServer*pSocketServer)
{
	singleton<ServerCallbackData>::instance().SetSocketServer(pSocketServer);
	return &(singleton<ServerCallbackData>::instance());
}*/